﻿using Landpy.CruiseControlNet.ConfigurationSidekick.ParserModule;
using Landpy.CruiseControlNet.FacadeInterface.ConfigurationModule;

namespace Landpy.CruiseControlNet.ConfigurationSidekick.ConfigTreeModule
{
    class GroupHandler : UpdateChildrenNode
    {
        public override void UpdateChildrenNodes(ChildrenNodeContext childrenNodeContext)
        {
            var treeNodeIndentity = this.GetTreeNodeIndentity(childrenNodeContext);
            var treeNodesBuilder = this.GetTreeNodesBuilder(childrenNodeContext.CruiseControl);
            if (treeNodeIndentity.Type == TreeNodeType.EmailGroup)
            {
                childrenNodeContext.SelectedNode.Nodes.Clear();
                EmailGroupKey emailGroupKey = EmailGroupKeyUtil.GetKey(treeNodeIndentity.Key);
                Group queryGroup = this.EmailGroupRepository.GetGroupById(childrenNodeContext.CruiseControl, emailGroupKey.ProjectId, emailGroupKey.EmailId, emailGroupKey.Id);
                var newNotificationsTreeNode = treeNodesBuilder.BuildNotificationsNode(queryGroup, childrenNodeContext.SelectedNode, emailGroupKey.ProjectId);
                childrenNodeContext.SelectedNode.Expand();
                newNotificationsTreeNode.Expand();
            }
            else
            {
                this.Successor.UpdateChildrenNodes(childrenNodeContext);
            }
        }
    }
}
